/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package myUtils;

import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;
import java.io.*;
import po.Activity;

/**
 *
 * @author luyang
 */
public class XMLParser
{
    public static String getXMLFromActivities(List list)
    {
        String xml="";
        Document document = DocumentHelper.createDocument();
        for(int i=0;i<list.size();++i)
        {
            Activity activity=(Activity) list.get(i);
            Element element=document.addElement("Activity");
            element.addAttribute("id", String.valueOf(activity.getId()));
            element.addAttribute("name", activity.getName());
        }
        xml=document.getText();
        System.out.println("xml is : \n"+xml);

        return xml;
    }

    public static String getErrorXML(String msg)
    {
        String xml="";
        Document document = getDocument();
        Element msg_element=document.getRootElement().addElement("message");
        msg_element.setText(msg);
        xml=document.asXML();
        System.out.println("xml is : \n"+xml);

        return xml;
    }
    /*public static Document getErrorXML(String msg)
    {
        String xml="";
        Document document = getDocument();
        Element msg_element=document.getRootElement().addElement("message");
        msg_element.setText(msg);
        xml=document.asXML();
        System.out.println("xml is : \n"+xml);

        return document;
    }*/

    public static Document getDocument()
    {
        Document document = DocumentHelper.createDocument();
        document.addElement("root");
        return document;
    }

}
